<?php

if (!isset($T)) $T = "test_";

$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzабвгдеёжзийклмнопрстуфхцчшщъыьэюя0123456789АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
$str_c = strlen($str) - 1;
$data   = ""; for($i=0, $c=rand(1000,100000); $i<$c; $i++) { $data  .=mb_substr($str, rand(0, $str_c), 1); }
$data_1 = ""; for($i=0, $c=rand(1000,100000); $i<$c; $i++) { $data_1.=mb_substr($str, rand(0, $str_c), 1); }


$self["class_mail_outbox"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_mail_outbox`
(
`oid` INTEGER NOT NULL auto_increment,
`to` VARCHAR(255) NOT NULL,
 PRIMARY KEY  (`oid`),
 INDEX `class_mail_outbox_0` (`to`)
) ENGINE=InnoDB
EOD;
$self["class_mail_outbox"]['after'][]   = "DROP TABLE `{$T}class_mail_outbox`";
$self["class_mail_outbox"]['queries'][] = "INSERT INTO {$T}class_mail_outbox(oid, `to`) VALUES (NULL, FLOOR(RAND() * 1000))";
$self["class_mail_outbox"]['queries'][] = "UPDATE {$T}class_mail_outbox SET `to` = FLOOR(RAND() * 1000)";
$self["class_mail_outbox"]['queries_args']['count'] = 10000;





$self["class_tag"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_tag`
(
`oid` INTEGER NOT NULL,
`community_oid` INTEGER NOT NULL,
`name` VARCHAR(100) NOT NULL,
`description` VARCHAR(255) NOT NULL,
 PRIMARY KEY  (`oid`),
 INDEX `class_tag_0` (`community_oid`,`name`)
) ENGINE=InnoDB
EOD;
$self["class_tag"]['after'][] = "DROP TABLE `{$T}class_tag`";




$self["class_pid_user"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_pid_user`
(
`oid` INTEGER NOT NULL,
`ns.sreg` VARCHAR(255) NOT NULL,
`sreg.nickname` VARCHAR(100) NOT NULL,
`sreg.fullname` VARCHAR(255) NOT NULL,
`sreg.dob` VARCHAR(10) NOT NULL,
`sreg.gender` VARCHAR(2) NOT NULL,
`sreg.postcode` VARCHAR(10) NOT NULL,
`sreg.language` VARCHAR(20) NOT NULL,
`sreg.country` VARCHAR(255) NOT NULL,
`ns.platonica` VARCHAR(255) NOT NULL,
`platonica.profile_url` VARCHAR(255) NOT NULL,
`platonica.avatars` TEXT NOT NULL,
`platonica.country_full_eng` VARCHAR(255) NOT NULL,
`platonica.country_full_national` VARCHAR(255) NOT NULL,
 PRIMARY KEY  (`oid`),
 INDEX `class_pid_user_0` (`platonica.profile_url`)
) ENGINE=InnoDB
EOD;
$self["class_pid_user"]['after'][] = "DROP TABLE `{$T}class_pid_user`";




$self["class_community"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_community`
(
`oid` INTEGER NOT NULL,
`title` VARCHAR(255) NOT NULL,
`mod` text NOT NULL,
`community_url` VARCHAR(255) NOT NULL,
`config` VARCHAR(255) NOT NULL,
`date` DATETIME NOT NULL,
 PRIMARY KEY  (`oid`),
 INDEX `class_community_0` (`title`),
 INDEX `class_community_1` (`community_url`),
 INDEX `class_community_2` (`date`)
) ENGINE=InnoDB
EOD;
$self["class_community"]['after'][] = "DROP TABLE `{$T}class_community`";




$self["class_tdi"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_tdi`
(
`oid` INTEGER NOT NULL,
`itype` VARCHAR(20) NOT NULL,
`title` VARCHAR(50) NOT NULL,
`url` VARCHAR(25) NOT NULL,
`color` VARCHAR(3) NOT NULL,
`community_oid` INTEGER NOT NULL,
 PRIMARY KEY  (`oid`),
 INDEX `class_tdi_0` (`itype`),
 INDEX `class_tdi_1` (`url`)
) ENGINE=InnoDB
EOD;
$self["class_tdi"]['after'][] = "DROP TABLE `{$T}class_tdi`";




$self["class_filestorage"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_filestorage`
(
`oid` INTEGER NOT NULL,
`path_to_file` VARCHAR(255) NOT NULL,
`mime_type` VARCHAR(100) NOT NULL,
`file_size` INTEGER NOT NULL,
`real_fname` VARCHAR(255) NOT NULL,
`ext` VARCHAR(20) NOT NULL,
 PRIMARY KEY  (`oid`),
 INDEX `class_filestorage_0` (`path_to_file`),
 INDEX `class_filestorage_1` (`mime_type`)
) ENGINE=InnoDB
EOD;
$self["class_filestorage"]['after'][] = "DROP TABLE `{$T}class_filestorage`";




$self["class_role"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_role`
(
`oid` INTEGER NOT NULL,
`name` VARCHAR(100) NOT NULL,
`title` VARCHAR(100) NOT NULL,
`level` INTEGER NOT NULL,
 PRIMARY KEY  (`oid`),
 INDEX `class_role_0` (`name`),
 INDEX `class_role_1` (`level`)
) ENGINE=InnoDB
EOD;
$self["class_role"]['after'][] = "DROP TABLE `{$T}class_role`";





$self["class_auth_digest"]['before'][]=<<<EOD
 CREATE TABLE `{$T}class_auth_digest`
(
`oid` INTEGER NOT NULL,
`user_oid` INTEGER NOT NULL,
`digest` VARCHAR(100) NOT NULL,
`type` INTEGER NOT NULL,
 PRIMARY KEY  (`oid`),
 INDEX `class_auth_digest_0` (`user_oid`,`type`)
) ENGINE=InnoDB

EOD;
for($i=0; $i<10000; $i++) {
	$oid      = $i;
	$user_oid = rand(0, 10000);
	$digest   = md5(rand(0, 10000) * time());
	$digests[$i] = $digest;
	$self["class_mail_outbox"]['before'][] = 
		"INSERT INTO {$T}class_auth_digest(oid, user_oid, digest, type) VALUES ({$oid}, {$user_oid}, '{$digest}', 0)";
	$didx = rand(0, $i);
	$self["class_auth_digest"]['queries'][] = "SELECT * FROM {$T}class_auth_digest WHERE digest = '{$digests[$didx]}'";
}
$self["class_mail_outbox"]['before'][] = "optimize table {$T}class_auth_digest";
$self["class_auth_digest"]['after'][]   = "DROP TABLE   `{$T}class_auth_digest`";
$self["class_auth_digest"]['queries_args']['dis'] = true;





$self["class_tdi_order"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_tdi_order`
(
`oid` INTEGER NOT NULL,
`community_oid` INTEGER NOT NULL,
`order` INTEGER NOT NULL,
 PRIMARY KEY  (`oid`,`community_oid`,`order`)
) ENGINE=InnoDB
EOD;
$self["class_tdi_order"]['after'][] = "DROP TABLE `{$T}class_tdi_order`";





$self["objects"]['before'][]=<<<EOD
CREATE TABLE `{$T}objects`
(
`oid` INTEGER NOT NULL auto_increment,
`type` VARCHAR(32) NOT NULL DEFAULT '',
`data` MEDIUMTEXT NOT NULL,
`ctime` TIMESTAMP NOT NULL,
`mtime` TIMESTAMP NOT NULL,
`is_deleted` TINYINT(1) NOT NULL,
 PRIMARY KEY  (`oid`),
 INDEX `objects_0` (`type`),
 INDEX `objects_1` (`ctime`),
 INDEX `objects_2` (`mtime`)
) ENGINE=InnoDB
EOD;
$self["objects"]['before'][]="SET @oid=0";

$self["objects"]['after'][] = "DROP TABLE `{$T}objects`";
$self["objects"]['queries'][] = "INSERT INTO {$T}objects(oid, `type`, `data`, `ctime`, `mtime`, `is_deleted`) VALUES (NULL, FLOOR(RAND() * 1000), '$data', NOW(), NOW(), 0)";
$self["objects"]['queries'][] = "SELECT @a:=FLOOR(RAND() * MAX(oid)) FROM {$T}objects";
$self["objects"]['queries'][] = "UPDATE {$T}objects SET `data`='$data_1', `mtime`=NOW() WHERE oid=@oid";
$self["objects"]['queries_args']['count'] = 10000;





$self["class_user"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_user`
(
`oid` INTEGER NOT NULL,
`login` VARCHAR(100) NOT NULL,
`realm` VARCHAR(100) NOT NULL,
`password` VARCHAR(100) NOT NULL,
`nick` VARCHAR(100) NOT NULL,
`fullname` VARCHAR(50) NOT NULL,
`status` VARCHAR(140) NOT NULL,
`bdate` VARCHAR(40) NOT NULL,
`avatar` VARCHAR(100) NOT NULL,
`sent_invitations` BOOL NOT NULL,
`email` VARCHAR(255) NOT NULL,
`openid_url` VARCHAR(255) NOT NULL,
 PRIMARY KEY  (`oid`),
 INDEX `class_user_0` (`login`,`password`),
 INDEX `class_user_1` (`openid_url`),
 INDEX `class_user_2` (`email`)
) ENGINE=InnoDB
EOD;
$self["class_user"]['after'][] = "DROP TABLE `{$T}class_user`";





$self["class_emails"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_emails`
(
`oid` INTEGER NOT NULL,
`email` text NOT NULL,
`user_oid` INTEGER DEFAULT NULL,
 PRIMARY KEY  (`oid`),
 INDEX `class_emails_0` (`oid`)
) ENGINE=InnoDB
EOD;
$self["class_emails"]['after'][] = "DROP TABLE `{$T}class_emails`";





$self["class_comment"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_comment`
(
`oid` INTEGER NOT NULL,
`text` text NOT NULL,
`posted_by_id` INTEGER NOT NULL,
`item_oid` INTEGER NOT NULL,
`is_deleted` BOOL NOT NULL,
`edited_user_oid` INTEGER DEFAULT NULL,
`edited_dtime` DATETIME DEFAULT NULL,
 PRIMARY KEY  (`oid`),
 INDEX `class_comment_0` (`posted_by_id`),
 INDEX `class_comment_1` (`item_oid`)
) ENGINE=InnoDB
EOD;
$self["class_comment"]['after'][] = "DROP TABLE `{$T}class_comment`";






$self["class_node"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_node`
(
`oid` INTEGER NOT NULL DEFAULT 123,
`title` VARCHAR(255) NOT NULL,
 PRIMARY KEY  (`oid`),
 INDEX `class_node_0` (`title`)
) ENGINE=InnoDB
EOD;
$self["class_node"]['after'][] = "DROP TABLE `{$T}class_node`";





$self["class_subscribers"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_subscribers`
(
`oid` INTEGER NOT NULL,
`email_oid` INTEGER NOT NULL,
`topic_oid` INTEGER NOT NULL,
 PRIMARY KEY  (`oid`,`email_oid`,`topic_oid`)
) ENGINE=InnoDB
EOD;
$self["class_subscribers"]['after'][] = "DROP TABLE `{$T}class_subscribers`";





$self["class_register"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_register`
(
`oid` INTEGER NOT NULL,
`param` VARCHAR(50) NOT NULL,
`val` VARCHAR(50) NOT NULL,
 PRIMARY KEY  (`oid`),
 INDEX `class_register_0` (`param`),
 INDEX `class_register_1` (`val`)
) ENGINE=InnoDB
EOD;
$self["class_register"]['after'][] = "DROP TABLE `{$T}class_register`";





$self["class_imagestorage"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_imagestorage`
(
`oid` INTEGER NOT NULL,
`mime_type` VARCHAR(100) NOT NULL,
`path_to_orig` VARCHAR(255) NOT NULL,
`orig_size_x` INTEGER NOT NULL,
`orig_size_y` INTEGER NOT NULL,
`path_to_sm1` VARCHAR(255),
`sm1_size_x` INTEGER,
`sm1_size_y` INTEGER,
`path_to_sm2` VARCHAR(255),
`sm2_size_x` INTEGER,
`sm2_size_y` INTEGER,
`path_to_gal` VARCHAR(255),
`gal_size_x` INTEGER,
`gal_size_y` INTEGER,
`path_to_wdg` VARCHAR(255),
`wdg_size_x` INTEGER,
`wdg_size_y` INTEGER,
 PRIMARY KEY  (`oid`)
) ENGINE=InnoDB
EOD;
$self["class_imagestorage"]['after'][] = "DROP TABLE `{$T}class_imagestorage`";





$self["class_doc"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_doc`
(
`oid` INTEGER NOT NULL,
`title` VARCHAR(255) NOT NULL,
`announce` VARCHAR(255),
`body` TEXT NOT NULL,
`tags` VARCHAR(255),
`posted_by_id` INTEGER,
`community_oid` INTEGER,
`tdi_oid` INTEGER,
`is_deleted` BOOL DEFAULT 0,
`author_url` VARCHAR(255) DEFAULT NULL,
`issue_oid` INTEGER DEFAULT 0,
`date` DATETIME NOT NULL,
 PRIMARY KEY  (`oid`)
) ENGINE=InnoDB
EOD;
$self["class_doc"]['after'][] = "DROP TABLE `{$T}class_doc`";





$self["class_image"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_image`
(
`oid` INTEGER NOT NULL,
`title` VARCHAR(255) NOT NULL,
`description` VARCHAR(255),
`tags` VARCHAR(255),
`image_oid` INTEGER,
`posted_by_id` INTEGER,
`community_oid` INTEGER,
`tdi_oid` INTEGER,
`is_deleted` BOOL DEFAULT 0,
`author_url` VARCHAR(255) DEFAULT NULL,
`issue_oid` INTEGER DEFAULT 0,
`date` DATETIME NOT NULL,
 PRIMARY KEY  (`oid`)
) ENGINE=InnoDB
EOD;
$self["class_image"]['after'][] = "DROP TABLE `{$T}class_image`";





$self["class_link"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_link`
(
`oid` INTEGER NOT NULL,
`title` VARCHAR(255) NOT NULL,
`description` VARCHAR(255),
`tags` VARCHAR(255),
`url` VARCHAR(255) NOT NULL,
`posted_by_id` INTEGER,
`community_oid` INTEGER,
`tdi_oid` INTEGER,
`is_deleted` BOOL DEFAULT 0,
`author_url` VARCHAR(255) DEFAULT NULL,
`issue_oid` INTEGER DEFAULT 0,
`date` DATETIME NOT NULL,
 PRIMARY KEY  (`oid`)
) ENGINE=InnoDB
EOD;
$self["class_link"]['after'][] = "DROP TABLE `{$T}class_link`";





$self["class_file"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_file`
(
`oid` INTEGER NOT NULL,
`title` VARCHAR(255) NOT NULL,
`description` VARCHAR(255),
`tags` VARCHAR(255),
`file_oid` INTEGER,
`posted_by_id` INTEGER,
`community_oid` INTEGER,
`tdi_oid` INTEGER,
`is_deleted` BOOL DEFAULT 0,
`author_url` VARCHAR(255) DEFAULT NULL,
`issue_oid` INTEGER DEFAULT 0,
`date` DATETIME NOT NULL,
 PRIMARY KEY  (`oid`)
) ENGINE=InnoDB
EOD;
$self["class_file"]['after'][] = "DROP TABLE `{$T}class_file`";





$self["class_issue"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_issue`
(
`oid` INTEGER NOT NULL,
`issue_number` VARCHAR(10) NOT NULL,
`publish_date` VARCHAR(20) NOT NULL,
`issue_name` VARCHAR(100),
`issue_descr` VARCHAR(255),
`posted_by_id` INTEGER,
`community_oid` INTEGER,
`tdi_oid` INTEGER,
`is_deleted` BOOL DEFAULT 0,
`date` DATETIME NOT NULL,
 PRIMARY KEY  (`oid`)
) ENGINE=InnoDB
EOD;
$self["class_issue"]['after'][] = "DROP TABLE `{$T}class_issue`";





$self["class_allfeed"]['before'][]=<<<EOD
CREATE TABLE `{$T}class_allfeed`
(
`oid` INTEGER NOT NULL,
`title` VARCHAR(255) NOT NULL,
`announce` VARCHAR(255),
`body` TEXT NOT NULL,
`tags` VARCHAR(255),
`posted_by_id` INTEGER,
`community_oid` INTEGER,
`tdi_oid` INTEGER,
`is_deleted` BOOL DEFAULT 0,
`author_url` VARCHAR(255) DEFAULT NULL,
`issue_oid` INTEGER DEFAULT 0,
`description` VARCHAR(255),
`image_oid` INTEGER,
`url` VARCHAR(255) NOT NULL,
`file_oid` INTEGER,
`issue_number` VARCHAR(10) NOT NULL,
`publish_date` VARCHAR(20) NOT NULL,
`issue_name` VARCHAR(100),
`issue_descr` VARCHAR(255),
`date` DATETIME NOT NULL,
 PRIMARY KEY  (`oid`)
) ENGINE=InnoDB
EOD;
$self["class_allfeed"]['after'][] = "DROP TABLE `{$T}class_allfeed`";
$self["class_allfeed"]['queries_args']['count'] = 10;





$self['t_acl_list']['before'][]=<<<EOD
CREATE TABLE `t_acl_list` (
		  `id` int(11) NOT NULL auto_increment,
		  `id_subject` int(11) NOT NULL,
		  `comment` varchar(50) collate utf8_bin NOT NULL,
		  `type` varchar(1) collate utf8_bin NOT NULL,
		  `action` varchar(35) collate utf8_bin NOT NULL,
		  `is_allowed` varchar(1) collate utf8_bin NOT NULL,
		  `id_acl_tree` int(11) NOT NULL,
		  PRIMARY KEY  (`id`)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Acl Lists for objects in acl_tree'
EOD;
$self['t_acl_list']['after'][] = "DROP TABLE `t_acl_list`";





$self['t_acl_tree']['before'][]=<<<EOD
CREATE TABLE `t_acl_tree` (
		  `id` int(11) NOT NULL auto_increment,
		  `id_object` int(11) NOT NULL,
		  `parent_id` int(11) NOT NULL,
		  `lft` int(11) NOT NULL,
		  `rgt` int(11) NOT NULL,
		  `is_inherit` varchar(1) collate utf8_bin NOT NULL,
		  `_comment` varchar(50) collate utf8_bin NOT NULL,
		  `is_deleted` tinyint(1) DEFAULT '0' NOT NULL,			
		  PRIMARY KEY  (`id`),
		  UNIQUE KEY `unique1` (`id_object`,`parent_id`)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Acl tree of objects'
EOD;
$self['t_acl_tree']['after'][] = "DROP TABLE `t_acl_tree`";





$self['t_subject_relations']['before'][]=<<<EOD
CREATE TABLE `t_subject_relations` (
		  `id` int(11) NOT NULL auto_increment,
		  `parent_id` int(11) NOT NULL,
		  `lft` int(11) NOT NULL,
		  `rgt` int(11) NOT NULL,
		  `comment` varchar(30) collate utf8_bin NOT NULL default '',
		  `var` varchar(15) collate utf8_bin NOT NULL default '',
		  `id_object` int(11) NOT NULL COMMENT 'entity storage identifier',
		  PRIMARY KEY  (`id`)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
EOD;
$self['t_subject_relations']['after'][] = "DROP TABLE `t_subject_relations`";
//$self['t_subject_relations']['queries'][] = "SELECT l1.* FROM t_subject_relations LEFT OUTER JOIN t_subject_relations l1 ON l1.parent_id = t_subject_relations.id WHERE t_subject_relations.id = 2 AND l1.id_object<>0";
$self['t_subject_relations']['queries'][] = "SELECT lft as myvar FROM t_subject_relations WHERE id = 11";
$self['t_subject_relations']['queries'][] = "UPDATE t_subject_relations SET rgt = rgt + 2 WHERE rgt > 3";
$self['t_subject_relations']['queries'][] = "UPDATE t_subject_relations SET lft = lft + 2 WHERE lft > 3";
$self['t_subject_relations']['queries'][] = "INSERT INTO t_subject_relations(parent_id, lft, rgt, id_object, comment) VALUES(11, 4, 5,6, 'Role administrator')";
//$self['t_subject_relations']['queries'][] = "SELECT DISTINCT `{$T}objects`.* FROM `{$T}objects`,`{$T}class_register` WHERE (`{$T}class_register`.`param` = 'node_idat') and `{$T}class_register`.oid=`{$T}objects`.oid";
//$self['t_subject_relations']['queries'][] = "SELECT lft as myvar FROM t_acl_tree WHERE id = 1";
//$self['t_subject_relations']['queries'][] = "UPDATE t_acl_tree SET rgt = rgt + 2 WHERE rgt > 1";
//$self['t_subject_relations']['queries'][] = "UPDATE t_acl_tree SET lft = lft + 2 WHERE lft > 1";
//$self['t_subject_relations']['queries'][] = "INSERT INTO t_acl_tree(id_object, parent_id, lft, rgt, is_inherit, _comment) VALUES(23,1, 2, 3, 1, 'New community: fadsf')";
$self['t_subject_relations']['queries_args']['count'] = 10000;





$self["tag_subject"]['before'][]=<<<EOD
CREATE TABLE `{$T}tag_subject` (
		  `tag_oid` int(11) NOT NULL,
		  `subject_oid` int(11) NOT NULL,
		  PRIMARY KEY  (`tag_oid`, `subject_oid`)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Acl Lists for objects in acl_tree'
EOD;
$self["tag_subject"]['after'][] = "DROP TABLE `{$T}tag_subject`";

